package com.example.locationreport.locationreport;

import android.app.Application;
import android.content.Context;

import android.content.Intent;
import android.util.Log;

import androidx.multidex.MultiDex;

import com.easysocket.EasySocket;
import com.easysocket.interfaces.conn.IConnectionManager;
import com.example.locationreport.locationreport.global.Constant;
import com.example.locationreport.locationreport.global.ConstantInfo;
import com.example.locationreport.locationreport.listener.CrashHandler;
import com.example.locationreport.locationreport.receiver.BluetoothBootBroadcastReceiver;
import com.example.locationreport.locationreport.utils.LogLocationUtils;
import com.example.locationreport.locationreport.utils.voice.SpeakUtils;
import com.lzy.okgo.OkGo;

import org.litepal.LitePal;

public class CrashApplication extends Application {

    private static final String TAG = CrashApplication.class.getSimpleName();

    private static Context mContext;

    private BluetoothBootBroadcastReceiver broadcastReceiver;

    private Thread mOdbThreadHeartbeat;

    @Override
    public void onCreate() {
        super.onCreate();
        Log.e(TAG, "CrashApplication----------------------->: onCreate");
        mContext = this;

        // 删除冗余日志
        LogLocationUtils.deleteLog();

        LogLocationUtils.writeLog(TAG, "Gis应用启动");

        //初始化云知声语音
        SpeakUtils.getInstance().init(mContext);
        LogLocationUtils.writeLog(TAG, "初始化云知声语音");

        // 初始化LitePal数据库
        LitePal.initialize(mContext);
        LogLocationUtils.writeLog(TAG, "初始化LitePal数据库");

        // 开机自启动
        broadcastReceiver = new BluetoothBootBroadcastReceiver();
        LogLocationUtils.writeLog(TAG, "开机自启动");

        //全局捕获异常并记录
        CrashHandler.getInstance().init();
        LogLocationUtils.writeLog(TAG, "全局捕获异常并记录");

        // 初始化OkGo
        OkGo.getInstance().init(this);
        LogLocationUtils.writeLog(TAG, "初始化OkGo");


        if (null == mOdbThreadHeartbeat) {
            mOdbThreadHeartbeat = new Thread(() -> {
                odbHeartbeatRunnable.run();
            });
            mOdbThreadHeartbeat.setPriority(Thread.MIN_PRIORITY);
            mOdbThreadHeartbeat.setName(Constant.CrashApplicationThread);
            mOdbThreadHeartbeat.start();
        }
    }

    public static Context getContext() {
        return mContext;
    }

    private Runnable odbHeartbeatRunnable = new Runnable() {
        //    private class TaskHeartbeat extends TimerTask {
        @Override
        public void run() {
            while (true) {
                LogLocationUtils.writeLog(TAG, "运行5秒心跳检测");
                if (MainActivity.mActivity == null) {
                    LogLocationUtils.writeLog(TAG, "mainActivity未渲染");
                    Intent mainActivity = new Intent(mContext, MainActivity.class);
                    mainActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    mContext.startActivity(mainActivity);
                    LogLocationUtils.writeLog(TAG, "拉起mainActivity");
                }
                try {
                    Thread.sleep(1000 * 5);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }
    };


    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(base);
        LogLocationUtils.writeLog(TAG, "启动attachBaseContext方法");
    }

}
